


# Please install OpenAI SDK first: `pip3 install openai`


"""
//提示：
deepseek-chat 模型已经升级为 DeepSeek-V3；deepseek-reasoner 模型为新模型 DeepSeek-R1。
思维链为deepseek-reasoner模型在给出正式回答之前的思考过程，其原理详见推理模型。
如未指定 max_tokens，默认最大输出长度为 4K。请调整 max_tokens 以支持更长的输出。
关于上下文缓存的细节，请参考DeepSeek 硬盘缓存。
表格中展示了优惠前与优惠后的价格。即日起至北京时间 2025-02-08 24:00，所有用户均可享受 DeepSeek-V3 API 的价格优惠。 在此之后，模型价格将恢复至原价。DeepSeek-R1不参与优惠。
deepseek-reasoner的输出 token 数包含了思维链和最终答案的所有 token，其计价相同。
"""

from openai import OpenAI



class DeepSeekAPI:
    def __init__(self, api_key: str, base_url: str):
        self.api_key = api_key
        self.base_url = base_url
        self.client = OpenAI(api_key=api_key, base_url=base_url)

    

class TestAPI(DeepSeekAPI):
    def __init__(self, api_key: str, base_url: str):
        super().__init__(api_key, base_url)



    def Demo(self):
        response = self.client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": "Hello"},
        ],
        stream=False
        )
        # print(response.__dir__())   
        print(response.choices[0].message.content)
        return "OK"
    

    # 测试提问
    def test_api1(self, messages) -> str:
        """
        **测试提问**
        deepseek-chat  聊天
        """
        try:
            client = self.client
            response = client.chat.completions.create(
                model="deepseek-chat",
                messages=messages,
                stream=False
            )
            # print(response)   
            # print("\n ========================================================= \n")
            try:
                response = response.choices[0].message.content
                return response
            except Exception as e:
                return "解析出错-》Error: " + str(e)
        except Exception as e:
            return "执行出错-》Error: " + str(e)
        # print(response.choices[0].message.content)
    

    def test_api2(self, messages) -> str:
        """
        **测试提问**
        deepseek-reasoner  推理
        """
        try:
            client = self.client
            response = client.chat.completions.create(
                model="deepseek-reasoner",
                messages=messages,
                stream=False
            )

            try:
                # response = response.choices[0].message.content
                return response
            except Exception as e:
                return "解析出错-》Error: " + str(e)
        except Exception as e:
            return "执行出错-》Error: " + str(e)
        # print(response.choices[0].message.content)